﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.Model;
using AiXiu.DAL;
using AiXiu.Common;


namespace AiXiu.BLL
{
    public class LoginBLL
    {
        #region 通过用户名登录
        /// <summary>
        /// 通过用户名登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public OperResult<TBUsers> LoginByName(string userName, string password)
        {
            //用户名密码的非空验证
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("用户名或密码不能为空！");
            }
            //通过用户名查询用户，若不存在则返回错误信息提示
            LoginDAL loginDAL = new LoginDAL();
            TBLogins login = loginDAL.getUserByName(userName);
            if (login == null)
            {
                return OperResult<TBUsers>.Failed("用户名不存在！");
            }
            //验证用户的密码
            SHAEncryption sHAEncryption = new SHAEncryption();
            if (sHAEncryption.SHA1Encrypt(password) != login.Password)
            {
                return OperResult<TBUsers>.Failed("密码错误！");
            }
            //调用BLL层中的登录方法，并返回结果
            UserBLL userBLL = new UserBLL();
            TBUsers users = userBLL.getInfo(login.Id);
            return OperResult<TBUsers>.Succeed(users);
        }
        #endregion
        #region 通过手机号登陆
        /// <summary>
        /// 通过手机号登陆
        /// </summary>
        /// <param name="Phone"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public OperResult<TBUsers> LoginByPhone(string Phone, string password)
        {
            //手机号密码的非空验证
            if (string.IsNullOrEmpty(Phone) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("手机号或密码不能为空！");
            }
            //通过手机号查询用户，若不存在则返回错误信息提示
            LoginDAL loginDAL = new LoginDAL();
            TBLogins login = loginDAL.getUserByPhone(Phone);
            if (login == null)
            {
                return OperResult<TBUsers>.Failed("手机号不存在！");
            }
            //验证用户的密码
            SHAEncryption sHAEncryption = new SHAEncryption();
            if (sHAEncryption.SHA1Encrypt(password) != login.Password)
            {
                return OperResult<TBUsers>.Failed("密码错误！");
            }
            //调用BLL层中的登录方法，并返回结果
            UserBLL userBLL = new UserBLL();
            TBUsers users = userBLL.getInfo(login.Id);
            return OperResult<TBUsers>.Succeed(users);
        }
        #endregion
        #region 注册用户
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="logins"></param>
        /// <returns></returns>
        public OperResult Register(TBLogins logins)
        {
            //用户名,手机号,密码的非空验证
            if (string.IsNullOrEmpty(logins.UserName))
            {
                return OperResult.Failed("用户名不能为空");
            }
            if (string.IsNullOrEmpty(logins.Password))
            {
                return OperResult.Failed("密码不能为空");
            }
            if (string.IsNullOrEmpty(logins.MobileNumber))
            {
                return OperResult.Failed("手机号不能为空");
            }

            //判断用户名和手机号是否已经被注册(调用LoginDAL中的方法)
            LoginDAL loginsDAL = new LoginDAL();
            if (loginsDAL.ExistsUserName(logins.UserName))
            {
                return OperResult.Failed("用户名已存在");
            }
            if (loginsDAL.ExistsUserPhone(logins.MobileNumber))
            {
                return OperResult.Failed("手机号已存在");
            }

            //加密用户密码
            SHAEncryption sSHAEncryption = new SHAEncryption();
            logins.Password = sSHAEncryption.SHA1Encrypt(logins.Password);
            //注册
            try
            {
                loginsDAL.Register(logins);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString());
                return OperResult.Failed("注册失败：" + ex.ToString());
            }
            //注册成功 返回结果
            return OperResult.Succeed();
        }
    }
}
          #endregion
